package com.seminario.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.seminario.model.Registro;
import com.seminario.util.HibernateUtil;

public class RegistroDao {

	public List<Registro> getAllRecords() {
		List<Registro> registros = new ArrayList<Registro>();
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			registros = session.createQuery("from Registro").list();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return registros;
	}

	public Registro getRecordById(int idU) {
		Registro registro = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String queryString = "from Registro where id = :id";
			Query query = session.createQuery(queryString);
			query.setInteger("id", idU);
			registro = (Registro) query.uniqueResult();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return registro;
	}

}
